// pages/SignIn/SignIn.js  15181129598
Page({

  data: {
    username:"15181140093",
    password:"111111",
    show:""
  },

  onLoad: function (options) {
    wx.getStorage({
      key: 'username',
      success (res) {
        console.log("获取到缓存，跳转到index",res)
        wx.redirectTo({
          url: '/pages/index/index'
        })
      },
      
      fail(res){
        // console.log("没有获取到data，进行登录",res)
      }
    })
    // wx.removeStorage({ 清除缓存
    //   key : 'key',
    //   success (res) {
    //     console.log(res)
    //   }
    // })
  },
  setUserNameValue:function(e){
    this.setData({username:e.detail.value})
  },
  setPasswordValue:function(e){
    this.setData({password:e.detail.value})
  },
  logOn(){
    var that = this
    if(that.data.username==""){
      wx.showToast({
        title: '请输入账号',
        icon: 'none', 
        duration: 2000     
      })  
    }else if(that.data.password==""){
      wx.showToast({
        title: '请输入密码',
        icon: 'none', 
        duration: 2000     
      })  
    }else{
      wx.request({
        url:'http://ypbitx.natappfree.cc/MyServlet/login',
        header: { 
          'content-type': 'application/json',
        },
        data: { 
          'password':that.data.password,
          'username':that.data.username
        },
        success(e){
          if(e.data=='登陆成功'){
            wx.setStorage({
              key:"username",
              data:that.data.username
            })
            // console.log("登录成功，缓存存入")
            //弹出登陆成功
            wx.showToast({
              title: '登陆成功',
              icon: 'none', 
              duration: 2000     
            })  
            //登陆成功跳转到首页
            wx.reLaunch({
              url: '/pages/entry/entry'
            })
          }else if(e.data=="此账号还未注册，请先注册"){
            wx.showToast({
              title: '此账号还未注册，请先注册',
              icon: 'none', 
              duration: 2000     
            })  
          }else{
            wx.showToast({
              title: '用户名或者密码错误',
              icon: 'none', 
              duration: 2000     
            })  
          }
        },
        fail(res){
          console.log("发起登录请求失败",res);
        }
      })  
    }
  },
  hideModal(){
    this.setData({show:""})
  },
  register(){
    wx.navigateTo({
      url: '/pages/register/register'
    })
  }
})